package cn.net.autocode.platform.roles.service;

import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.dbManager.IdWorker;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("platformRoleServ")
public class RoleServ {
    private final Dao dao;

    public RoleServ(Dao dao) {
        this.dao = dao;
    }


    public void saveRoleMenus(JSONObject param){
        String roleId = param.getString("roleId");
        JSONArray checkedKeys = param.getJSONArray("checkedKeys");

        //删除最顶层的菜单
        String sql1 = "select distinct parent_id from sys_menus";
        List<String> parentIds = dao.queryListValue(sql1,String.class);

        List<String> leafMenus = new ArrayList<>();
        for(int i=0;i<checkedKeys.size();i++){
            String menuId = checkedKeys.getString(i);
            if(!parentIds.contains(menuId)){
                leafMenus.add(menuId);
            }
        }

        //删除角色中不存在的menuId
        if (!leafMenus.isEmpty()) {
            dao.executeUpdate("delete from sys_role_menus where role_id=? and menu_id not in (?)",roleId,leafMenus);
        }else{
            dao.executeUpdate("delete from sys_role_menus where role_id=?",roleId);
        }

        //插入新的菜单id
        List<String> existsMenus = dao.queryListValue("select menu_id from sys_role_menus where role_id=?",String.class,roleId);
        leafMenus.removeAll(existsMenus);
        for(String menuId:leafMenus){
            dao.executeUpdate("insert into sys_role_menus(id,role_id,menu_id) values(?,?,?)", IdWorker.ulid(),roleId,menuId);
        }

    }

    public void saveAppRoleMenus(JSONObject param){
        String roleId = param.getString("roleId");
        JSONArray checkedKeys = param.getJSONArray("checkedKeys");

        //删除最顶层的菜单
        String sql1 = "select distinct parent_id from app_menus";
        List<String> parentIds = dao.queryListValue(sql1,String.class);

        List<String> leafMenus = new ArrayList<>();
        for(int i=0;i<checkedKeys.size();i++){
            String menuId = checkedKeys.getString(i);
            if(!parentIds.contains(menuId)){
                leafMenus.add(menuId);
            }
        }

        //删除角色中不存在的menuId
        if (!leafMenus.isEmpty()) {
            dao.executeUpdate("delete from app_role_menus where role_id=? and menu_id not in (?)",roleId,leafMenus);
        }else{
            dao.executeUpdate("delete from app_role_menus where role_id=?",roleId);
        }

        //插入新的菜单id
        List<String> existsMenus = dao.queryListValue("select menu_id from app_role_menus where role_id=?",String.class,roleId);
        leafMenus.removeAll(existsMenus);
        for(String menuId:leafMenus){
            dao.executeUpdate("insert into app_role_menus(id,role_id,menu_id) values(?,?,?)", IdWorker.ulid(),roleId,menuId);
        }
    }

    public List<String> defaultRole(String orgCode){
        if (orgCode == null || orgCode.isBlank()) {
            return dao.queryListValue("select id from sys_roles where is_default='Y' and is_public='Y'",String.class);
        }else{
            return dao.queryListValue("select id from sys_roles where is_default='Y' and ( is_public='Y' or org_coe=?)",String.class,orgCode);
        }
    }
}
